/**
 * Created by sunrui on 2018/6/29.
 */
import MYCONF from '../myconf'
import Vue from 'vue'

export default {
  namespaced: true,

  state: {
    realtime: {
      dataLoaded: false,
      time: 0,
      dis: 0,
      num: 0,
    },
    bus: {
      dataLoaded: false,
      num: 0,
      stops: []
    },
    taxi: {
      dataLoaded: false,
      num: 0
    },
    odData: [],
    odBus: {
      stops: [],
      lines: []
    },
  },

  getters: {
    realtime: state => state.realtime,
    bus: state => state.bus,
    taxi: state => state.taxi,
    odData: state => state.odData,
    odBus: state => state.odBus
  },

  mutations: {
    setAnalysiseData(state, payload) {
      if (payload.realtime) {
        state.realtime.dataLoaded = true;
        state.realtime.time = payload.realtime.time;
        state.realtime.dis = payload.realtime.dis;
        state.realtime.num = payload.realtime.num;
      }

      if (payload.bus) {
        state.bus.dataLoaded = true;
        state.bus.num = payload.bus.num;
        state.bus.stops = payload.bus.stops;
      }

      if (payload.taxi) {
        state.taxi.dataLoaded = true;
        state.taxi.num = payload.taxi.num;
      }
    },
    setOdData(state, payload) {
      state.odData = payload;
    },
    setOdBus(state, payload) {
      state.odBus.stops = payload.stops;
      state.odBus.lines = payload.lines;
    }
  },

  actions: {
    getAnalysiseDataData(context) {
      Vue.http.get(MYCONF.OD_ANALYSISE_DATA_URL)
        .then(response => {
          response.json().then(json => {
            context.commit('setAnalysiseData', json);
          });
        }, response => {
          console.error("analysise data request error", response.status)
        });
    },
    getOdData(context){
      Vue.http.get(MYCONF.OD_MAP_DATA_URL)
        .then(response => {
          response.json().then(json => {
            context.commit('setOdData', json);
          });
        }, response => {
          console.error("od data request error", response.status)
        });
    },
    getOdBus(context){
      Vue.http.get(MYCONF.OD_BUS_DATA_URL)
        .then(response => {
          response.json().then(json => {
            context.commit('setOdBus', json);
          });
        }, response => {
          console.error("bus data request error", response.status);
        });
    }
  }
}
